package main

/*
练习使用mysql
*/
import (
	"database/sql"
	/*会自动注册驱动mysql*/
	"fmt"
	_ "github.com/go-sql-driver/mysql"
)

type DbWorker struct {
	//mysql data source name
	Dsn string
}

func main() {
	dbw := DbWorker{
		Dsn: "root:123456@tcp(127.0.0.1:3307)/wp",
	}
	db, err := sql.Open("mysql", dbw.Dsn)

	if err != nil {
		panic(err)
		return
	}
	defer db.Close()

	fmt.Println("open mysql")

	err = db.Ping()
	if err != nil {
		panic(err)
		return
	}

	fmt.Println("ping mysql")

	rows, err := db.Query("SELECT  `id`,  `name`,  `title`,  `field`,  `type`,  `value`,  `remark`,  `is_show`,  LEFT(`extra`, 256),  `model_id`,  `model_name`,  `is_must`,  `status`,  `update_time`,  `create_time`,  `validate_rule`,  `validate_time`,  `error_info`,  `validate_type`,  `auto_rule`,  `auto_time`,  `auto_type` FROM `wp`.`wp_attribute` LIMIT 3")
	defer rows.Close()
	if err != nil {
		panic(err)
		return
	}

	//m1 := make(map[string]interface{})
	//m1["id"] = nil
	//m1["name"] = nil
	var id int64
	var name string
	var title string
	for rows.Next() {
		rows.Scan(&id, name, title)
		if err != nil {
			panic(err)
			return
		}
		fmt.Println("id=>", id, " name=>", name, " title=>", title)
	}
}
